﻿using UnityEngine;
using System.Collections;

public class CloneBehaviour : MonoBehaviour
{
	private static object[] m_massMatrix;
	private static int m_numTotalClones = 0;
	private static int m_matrixHeight = 51;
	private static int m_matrixWidth = 51;

	private int m_maxNumClones;
	private int m_numClones = 0;
	public int NumClones 
	{
		get{ return m_numClones;}
	}

	private int m_matrixIndex;
	public int MatrixIndex
	{
		get{ return m_matrixIndex;}
	}

	
	// Use this for initialization
	void Start ()
	{
		if (m_numTotalClones == 0)
		{
			if (m_massMatrix == null)
					m_massMatrix = new object[m_matrixWidth * m_matrixHeight];

			m_matrixIndex = 1301;
			m_massMatrix [1301] = this.gameObject;
			m_numTotalClones++;
			m_maxNumClones = 4;
		}
		else
		{
			m_maxNumClones = 1;
			m_maxNumClones += (new System.Random().Next(0, 10)) < 2 ? 1 : 0;
		}
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (m_numClones < m_maxNumClones &&
		    m_numTotalClones < 2601)
		{
			//WaitForSeconds(1);
			//Check positions around.
			//Collect possible possitions
			System.Collections.Generic.List<SPAWNDIRECTION> possibleSpawnPoints = new System.Collections.Generic.List<SPAWNDIRECTION>();
			
			//Check West.
			if((m_matrixIndex % m_matrixWidth) != 0)
			{
				if(m_massMatrix[m_matrixIndex - 1] == null)
					possibleSpawnPoints.Add(SPAWNDIRECTION.WEST);
			}
			
			//Check East
			if((m_matrixIndex % m_matrixWidth) != m_matrixWidth-1)
			{
				if(m_massMatrix[m_matrixIndex+1] == null)
					possibleSpawnPoints.Add(SPAWNDIRECTION.EAST);
			}
			
			//Check North
			if((m_matrixIndex / m_matrixHeight) != 0)
			{
				if(m_massMatrix[m_matrixIndex - m_matrixHeight] == null)
					possibleSpawnPoints.Add(SPAWNDIRECTION.NORTH);
			}
			
			//Check South
			if((m_matrixIndex / m_matrixHeight) != m_matrixHeight-1)
			{
				if(m_massMatrix[m_matrixIndex + m_matrixHeight] == null)
					possibleSpawnPoints.Add(SPAWNDIRECTION.SOUTH);
			}
			
			//Randomize a spawning position.
			
			//If no possible spawn points where found.
			if(possibleSpawnPoints.Count == 0)
				return;
			
			Transform newCloneTransform = Instantiate (	this.transform, this.transform.position, Quaternion.identity) as Transform;
			CloneBehaviour newClone = newCloneTransform.gameObject.GetComponents<Component>()[newCloneTransform.GetComponents<Component>().Length-1] as CloneBehaviour;
			
			float translateDistace = 0.25f;
			System.Random spawnPoint = new System.Random();
			switch(possibleSpawnPoints[spawnPoint.Next(0, possibleSpawnPoints.Count)])
			{
			case SPAWNDIRECTION.NORTH:
				newClone.m_matrixIndex = this.m_matrixIndex - m_matrixWidth;
				newClone.transform.Translate(0,-translateDistace, 0);
				break;
			case SPAWNDIRECTION.SOUTH:
				newClone.m_matrixIndex = this.m_matrixIndex + m_matrixWidth;
				newClone.transform.Translate(0, translateDistace, 0);
				break;
			case SPAWNDIRECTION.WEST:
				newClone.m_matrixIndex = this.m_matrixIndex - 1;
				newClone.transform.Translate(-translateDistace, 0, 0);
				break;
			case SPAWNDIRECTION.EAST:
				newClone.m_matrixIndex = this.m_matrixIndex + 1;
				newClone.transform.Translate(translateDistace, 0, 0);
				break;
			}
			
			m_massMatrix[newClone.m_matrixIndex] = newClone;
			
			/*
			Debug.Log ("///////");
			Debug.Log ("CurrentClone_MatrixIndex = " + m_matrixIndex);
			Debug.Log ("CurrentClone_this.m_numClones = " + m_numClones.ToString());			
			Debug.Log ("CurrentClone_position = " + transform.position.x + " ; " + transform.position.y);
			Debug.Log ("CurrentClone_CloneBehaviour.m_numTotalClones = " + m_numTotalClones);			
			Debug.Log ("///////");
			//*/
			
			m_numClones++;
			m_numTotalClones++;
			
			/*
			Debug.Log ("///////");
			Debug.Log ("NewClone_MatrixIndex = " + newClone.MatrixIndex.ToString());
			Debug.Log ("NewClone_this.m_numClones = " + newClone.ToString());			
			Debug.Log ("NewClone_position = " + newClone.transform.position.x + " ; " + newClone.transform.position.y);
			Debug.Log ("NewClone_CloneBehaviour.m_numTotalClones = " + m_numTotalClones);			
			Debug.Log ("///////");
			//*/
			
		}
	}

	enum SPAWNDIRECTION
	{
		NORTH,
		SOUTH,
		WEST,
		EAST,
	}

	void Clone()
	{

	}
}
